

import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import '../style/tokens/colors.dart';
import '../style/tokens/spacing.dart';
import '../style/tokens/typography.dart';
import '../style/tokens/borders.dart';

class ToastUtil {

  /// 显示全局Toast消息
  /// [message] 消息内容
  /// [duration] 显示时长
  /// [type] Toast类型
  static void showToast(
      String message, {
        Duration duration = const Duration(seconds: 3),
        UIToastType type = UIToastType.info,
      }) {
    print("message:${message}");
    SmartDialog.show(
      alignment: Alignment.topCenter,
      displayTime: duration,
      usePenetrate: true,
      clickMaskDismiss: false,
      builder: (context) => UITopToastWidget(
        message: message,
        type: type,
      ),
    );
  }

  /// 显示成功Toast
  static void showSuccess(String message) {
    showToast(message, type: UIToastType.success);
  }

  /// 显示错误Toast
  static void showError(String message) {
    showToast(message, type: UIToastType.error);
  }

  /// 显示警告Toast
  static void showWarning(String message) {
    showToast(message, type: UIToastType.warning);
  }

  /// 显示信息Toast
  static void showInfo(String message) {
    showToast(message, type: UIToastType.info);
  }
}

/// Toast类型枚举
enum UIToastType {
  success,
  error,
  warning,
  info,
}


/// 顶部Toast组件
class UITopToastWidget extends StatefulWidget {
  const UITopToastWidget({
    super.key,
    required this.message,
    this.type = UIToastType.info,
  });

  final String message;
  final UIToastType type;

  @override
  State<UITopToastWidget> createState() => _UITopToastWidgetState();
}

class _UITopToastWidgetState extends State<UITopToastWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<Offset> _slideAnimation;
  late Animation<double> _fadeAnimation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 400),
      vsync: this,
    );

    _slideAnimation = Tween<Offset>(
      begin: const Offset(0, -1),
      end: Offset.zero,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeOutBack,
    ));

    _fadeAnimation = Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeOut,
    ));

    _animationController.forward();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;

    Color backgroundColor;
    Color textColor;
    Color borderColor;
    IconData icon;

    switch (widget.type) {
      case UIToastType.success:
        backgroundColor = UIColors.successContainer;
        textColor = UIColors.onSuccessContainer;
        borderColor = UIColors.success;
        icon = Icons.check_circle_outline;
        break;
      case UIToastType.error:
        backgroundColor = colorScheme.errorContainer;
        textColor = colorScheme.onErrorContainer;
        borderColor = colorScheme.error;
        icon = Icons.error_outline;
        break;
      case UIToastType.warning:
        backgroundColor = UIColors.warningContainer;
        textColor = UIColors.onWarningContainer;
        borderColor = UIColors.warning;
        icon = Icons.warning_amber_outlined;
        break;
      case UIToastType.info:
        backgroundColor = UIColors.infoContainer;
        textColor = UIColors.onInfoContainer;
        borderColor = UIColors.info;
        icon = Icons.info_outline;
        break;
    }

    return Container(
      margin: EdgeInsets.only(
        top: MediaQuery.of(context).padding.top + UISpacing.sm,
        left: UISpacing.md,
        right: UISpacing.md,
      ),
      child: SlideTransition(
        position: _slideAnimation,
        child: FadeTransition(
          opacity: _fadeAnimation,
          child: Material(
            elevation: 4,
            borderRadius: BorderRadius.circular(UIBorders.radiusLG),
            shadowColor: borderColor.withOpacity(0.3),
            child: Container(
              width: double.infinity,
              padding: const EdgeInsets.all(UISpacing.md),
              decoration: BoxDecoration(
                color: backgroundColor,
                borderRadius: BorderRadius.circular(UIBorders.radiusLG),
                border: Border.all(
                  color: borderColor.withOpacity(0.3),
                  width: 1,
                ),
              ),
              child: Row(
                children: [
                  Icon(
                    icon,
                    color: borderColor,
                    size: 22,
                  ),
                  const SizedBox(width: UISpacing.sm),
                  Expanded(
                    child: Text(
                      widget.message,
                      style: UITextStyles.bodyMedium.copyWith(
                        color: textColor,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                  // 关闭按钮
                  GestureDetector(
                    onTap: () => SmartDialog.dismiss(),
                    child: Container(
                      padding: const EdgeInsets.all(2),
                      child: Icon(
                        Icons.close,
                        color: textColor.withOpacity(0.6),
                        size: 18,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
